package com.team9.fitness.controller;

import com.team9.fitness.service.ZRDDSSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 帖子点赞控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/post-like")
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
public class PostLikeController {

    @Autowired
    private ZRDDSSendService zrddsSendService;

    /**
     * 切换帖子点赞状态
     */
    @PostMapping("/toggle")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> togglePostLike(@RequestParam Long userId,
            @RequestParam Long postId) {

        log.info("切换帖子点赞状态请求: userId={}, postId={}", userId, postId);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("userId", userId);
        requestBody.put("postId", postId);

        return zrddsSendService.sendBusinessRequest("POST_LIKE_TOGGLE", "POST", "/api/post-like/toggle",
                userId.toString(), requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "点赞状态切换成功");
                        result.put("data", response.getData());
                        log.info("点赞状态切换成功: userId={}, postId={}", userId, postId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "点赞状态切换失败");
                        log.warn("点赞状态切换失败: userId={}, postId={}, error={}", userId, postId, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("点赞状态切换异常: userId={}, postId={}", userId, postId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "点赞状态切换服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取用户点赞状态
     */
    @GetMapping("/status")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getLikeStatus(@RequestParam Long userId,
            @RequestParam Long postId) {

        log.info("获取用户点赞状态请求: userId={}, postId={}", userId, postId);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("userId", userId);
        requestBody.put("postId", postId);

        return zrddsSendService.sendBusinessRequest("POST_LIKE_GET_STATUS", "GET", "/api/post-like/status",
                userId.toString(), requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取点赞状态成功");
                        result.put("data", response.getData());
                        log.info("获取点赞状态成功: userId={}, postId={}", userId, postId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取点赞状态失败");
                        log.warn("获取点赞状态失败: userId={}, postId={}, error={}", userId, postId, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取点赞状态异常: userId={}, postId={}", userId, postId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取点赞状态服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取帖子点赞数量
     */
    @GetMapping("/count/{postId}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getLikeCount(@PathVariable Long postId) {

        log.info("获取帖子点赞数量请求: postId={}", postId);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("postId", postId);

        return zrddsSendService.sendBusinessRequest("POST_LIKE_GET_COUNT", "GET", "/api/post-like/count/" + postId,
                null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取点赞数量成功");
                        result.put("data", response.getData());
                        log.info("获取点赞数量成功: postId={}", postId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取点赞数量失败");
                        log.warn("获取点赞数量失败: postId={}, error={}", postId, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取点赞数量异常: postId={}", postId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取点赞数量服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }
}
